package com.anhtuan.reminder.tasks;

import java.util.Calendar;

import com.anhtuan.reminder.services.NotifyService;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class SetAlarmTask implements Runnable {
	
	// The date selected for alarm
	private final Calendar date;
	// Time to repeat the alarm
	private final long intervalMillis;
	// System alarm manager
	private final AlarmManager alarmManager;
	// Context to retrieve the alarm manager
	private final Context context;
	
	
	public SetAlarmTask(Context context, Calendar date, long intervalMillis) {
		this.context = context;
		this.alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		this.date = date;
		this.intervalMillis = intervalMillis;
	}
	
	@Override
	public void run() {
		
		Intent intent = new Intent(context, NotifyService.class);
		intent.putExtra(NotifyService.INTENT_NOTIFY, true);
		PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
		
		// Set an alarm
		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), intervalMillis, pendingIntent);
	}

}
